<template>
  <BasicModal v-bind="$attrs" @register="register" :title="displayModuleTitle" :showOkBtn="false">
    <div>
      <a-button @click="onReGenerateReportButtonClick" style="margin: 10px 10px 10px 90px">{{
        record?.rptid > 0 ? '重新生成报告' : '生成报告'
      }}</a-button>
      <a v-if="record?.rptid > 0" @click="openReport()">查看报告</a>
    </div>

    <BasicForm @register="registerForm">
      <template #upload="{ model, field }">
        <a-button @click="onOkButtonClick">提交报告</a-button>
      </template>
    </BasicForm>
  </BasicModal>
</template>
<script lang="ts">
  import { computed, defineComponent, ref, unref } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { displayTitle } from '/@/utils/QcUnifyUtil';
  import { OperationTypeEnum } from '/@/enums/appEnum';
  import { QcUnifyTransToChildProps } from '/#/QcUnify';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { recordOperateModalSchemas } from './troubleCorrect.data';
  import {
    uploadEhsTroubleCorrectReport,
    reportEhsTroubleCorrect,
  } from '/@/api/ehs/troubleCorrect';
  import { getFileApi } from '/@/api/platform/file';
  import { useGlobSetting } from '/@/hooks/setting';
  const { uploadUrl } = useGlobSetting();
  const { createMessage, createConfirm } = useMessage();
  export default defineComponent({
    components: { BasicModal, BasicForm },
    name: 'RecordOperateModal',
    emits: ['success'],
    setup(props, { emit }) {
      const operateType = ref(OperationTypeEnum.UN_KNOWN);
      const [registerForm, { setFieldsValue, resetFields, validate, updateSchema }] = useForm({
        labelWidth: 90,
        schemas: recordOperateModalSchemas,
        showActionButtonGroup: false,
        actionColOptions: {
          span: 24,
        },
      });
      const qcUnifyProps = ref<QcUnifyTransToChildProps>();
      const record = ref({});
      const [register, { closeModal, changeLoading }] = useModalInner((data) => {
        if (data != undefined && data != null) {
          let unrefData = unref(data);
          qcUnifyProps.value = unrefData;
          //接收父组件传递的数据
          if (unrefData?.operateType) {
            //判断是否传入op属性
            operateType.value = unrefData.operateType;
          }
          record.value = data.record;
        }
      });

      function onReGenerateReportButtonClick() {
        if (record.value.rptid > 0) {
          createConfirm({
            title: '提示',
            content: '当前已有对应的报告文件，是否重新生成并替换已有的报告文件?',
            onOk: () => {
              changeLoading(true);
              reportEhsTroubleCorrect(record.value.correctid).then((res) => {
                if (res) {
                  record.value.rptid = res;
                  createMessage.success('重新生成报告成功');
                } else {
                  createMessage.error('重新生成报告失败');
                }
                changeLoading(false);
              });
            },
          });
        } else {
          changeLoading(true);
          reportEhsTroubleCorrect(record.value.correctid).then((res) => {
            if (res) {
              record.value.rptid = res;
              createMessage.success('重新生成报告成功');
            } else {
              createMessage.error('重新生成报告失败');
            }
            changeLoading(false);
          });
        }
      }

      function openReport() {
        window.open(uploadUrl + getFileApi + record.value.rptid);
      }

      async function onOkButtonClick() {
        const value = await validate();
        if (value.attachment) {
          uploadEhsTroubleCorrectReport({
            id: record.value.correctid,
            fileid: value.attachment[0].id,
          }).then((res) => {
            if (!res) {
              createMessage.success('上传报告成功');
              emit('success');
              closeModal();
            }
          });
        } else {
          createMessage.error('请上传报告');
        }
      }

      let displayModuleTitle = computed(() => {
        return displayTitle(qcUnifyProps.value);
      });
      return {
        record,
        register,
        registerForm,
        closeModal,
        displayModuleTitle,
        onOkButtonClick,
        openReport,
        onReGenerateReportButtonClick,
      };
    },
  });
</script>
